package com.hqyj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.EmployeeInformationMapper;
import com.hqyj.dao.PersonnelTransferMapper;
import com.hqyj.pojo.EmployeeInformation;
import com.hqyj.pojo.PersonnelTransfer;
import com.hqyj.service.PersonnelTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;

@Service
public class PersonnelTransferServiceImpl extends ServiceImpl<PersonnelTransferMapper,PersonnelTransfer> implements PersonnelTransferService {
    @Autowired(required = false)
    PersonnelTransferMapper personnelTransferMapper;

    @Autowired(required = false)
    EmployeeInformationMapper employeeInformationMapper;

    @Override
    public HashMap<String,Object> select(PersonnelTransfer p) {
        System.out.println(p.getSort());
        /*HashMap<String, Object> map = new HashMap<>();
        List<PersonnelTransfer> list = personnelTransferMapper.selectList(null);
        map.put("list",list);
        System.out.println(map);
        return map;*/
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<PersonnelTransfer> page = new Page<>(p.getPage(),p.getRow());
        //条件构造器
        QueryWrapper<PersonnelTransfer> queryWrapper = new QueryWrapper<>();
        //根据id查询
        queryWrapper.eq(p.getId()!=null,"id",p.getId());
        //模糊查询
        queryWrapper.like(p.getEmployeeId()!=null&&!p.getEmployeeId().equals(""),"employee_id",p.getEmployeeId());
        queryWrapper.like(p.getName()!=null&&!p.getName().equals(""),"name",p.getName());

        if (p.getSort().equals("asc")) {
            //升序
            queryWrapper.orderByAsc("transfer_time");

        } else if (p.getSort().equals("desc")) {
            //降序
            queryWrapper.orderByDesc("transfer_time");

        }

        //查询数据库
        Page<PersonnelTransfer> eList =personnelTransferMapper.selectPage(page,queryWrapper);


        //当前页集合
        map.put("list",eList.getRecords());
        //总页数
        map.put("totalPage",eList.getPages());
        //总条数
        map.put("total",eList.getTotal());
        //当前页
        map.put("curPage",p.getPage());
        if(p.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",p.getPage()-1);

        }
        if(p.getPage()==eList.getPages()){
            //下一页
            map.put("nextPage",eList.getPages());

        }else{
            map.put("nextPage",p.getPage()+1);

        }

        //每页显示条数
        map.put("row",p.getRow());

        return map;
    }



    @Override
    public HashMap<String, Object> add(PersonnelTransfer p) {
        int insert = personnelTransferMapper.insert(p);
        //同时修改EmployeeInformation表里的字段
        EmployeeInformation employeeInformation = new EmployeeInformation();
        employeeInformation.setEmployeeId(p.getEmployeeId());
        employeeInformation.setDepartment(p.getNewDepartment());
        employeeInformation.setPost(p.getNewPosition());

        //调用修改方法
        Integer integer = employeeInformationMapper.updatebyEmployeeId(employeeInformation);
        HashMap<String, Object> map = new HashMap<>();
        if(insert>0){
            map.put("info","添加成功");
        }else{
            map.put("info","添加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(PersonnelTransfer p) {
        //调用修改方法
        int i = personnelTransferMapper.updateById(p);
        //System.out.println(p);

        //同时修改EmployeeInformation表里的字段
        EmployeeInformation employeeInformation = new EmployeeInformation();
        employeeInformation.setEmployeeId(p.getEmployeeId());
        employeeInformation.setDepartment(p.getNewDepartment());
        employeeInformation.setPost(p.getNewPosition());

       //调用修改方法
        Integer integer = employeeInformationMapper.updatebyEmployeeId(employeeInformation);
        //System.out.println(integer);


        HashMap<String, Object> map = new HashMap<String, Object>();
        if(i>0){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer a) {
        int i = personnelTransferMapper.deleteById(a);
        HashMap<String, Object> map = new HashMap<>();
        if(i>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
}

    @Override
    public HashMap<String, Object> show(PersonnelTransfer p) {
        List<PersonnelTransfer> show = personnelTransferMapper.show();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",show);
        return map;
    }
}
